<template>
  <div class="login-container flex">
    <div class="login-left">
      <div class="login-left-box">
        <img :src="loginMain" alt="登录背景" />
      </div>
    </div>
    <div class="login-right flex">
      <div class="login-right-logo">
        <img src="@/assets/images/global/logo.png" alt="平台Logo" />
        <div class="login-right-logo-text">
          <span class="login-right-logo-text-span">制造执行系统MES v2024</span>
        </div>
      </div>
      <div class="login-main">
        <div class="login-title">
          <span>统一身份登录</span>
        </div>
        <div class="login-form">
          <el-form ref="formRef" :rules="rules" :model="formData" label-position="top" class="el-form login-form"
            @keyup.enter="handleSubmit" size="default">
            <el-form-item label="组织" prop="userName">
              <el-select v-model="formData.tenantId" class="w100" clearable placeholder="请选择">
                <el-option v-for="item in tenantData" :key="item.code" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="工号" prop="userName">
              <el-input v-model="formData.userCode" autocomplete="off" clearable placeholder="请输入"
                prefix-icon="el-icon-user" type="text" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="formData.password" :show-password="true" autocomplete="off" placeholder="请输入"
                prefix-icon="el-icon-lock" type="password" />
            </el-form-item>
            <el-form-item style="margin: 40px 0px 0">
              <el-button :loading="loading" class="login-submit" type="primary" @click="handleSubmit">
                <span>登 录</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="login-right-warp-main-footer">技术支持: 北京宏博远达科技有限公司</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { apiGetTenantList, apiLogin } from '@/apis/login'
import { useUserStore } from '@/store/user'
import { useSystemStore } from '@/store/system.js'
import { useRouterStore } from '@/store/route.js'
import { computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { addTrendsRouteList } from '@/router/utils.js'
import loginMain from "@/assets/images/login/login-main.svg";

const router = useRouter()
// 方便调试，后期去掉
const formData = reactive({
  tenantId: undefined,
  userCode: 'weidy',
  password: '1',
})

const rules = computed(() => {
  return {
    tenantId: [
      {
        required: formData.userCode !== 'root',
        message: '请选择组织',
        trigger: 'change',
      },
    ],
    userCode: [
      {
        required: true,
        message: '请输入用户名',
        trigger: 'blur',
      },
    ],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  }
})
const tenantData = ref({})

// 表单ref
const formRef = ref()
// 登录按钮
const loading = ref(false)
const handleSubmit = async () => {
  const { token } = storeToRefs(useUserStore())
  const { getUserInfo } = useUserStore()
  const { routerTagList } = storeToRefs(useSystemStore())
  const { routes } = storeToRefs(useRouterStore())
  await formRef.value.validate()
  loading.value = true
  try {
    const { accessToken } = await apiLogin(formData)
    token.value = accessToken
    routerTagList.value = []
    routes.value = []
    // 获取用户信息
    await getUserInfo()
    // 获取路由
    await addTrendsRouteList()
    ElMessage.success('登录成功')
  } finally {
    loading.value = false
  }
  router.push('/home')
}
const getTenantList = async () => {
  tenantData.value = await apiGetTenantList()
  if (tenantData.value.length === 1) {
    formData.tenantId = tenantData.value[0]['id']
  }

}
getTenantList()
</script>

<style lang="scss" scoped>
.login-container {
  height: 100%;
  width: 100%;
  overflow: hidden;
  position: relative;
  background: #1f76cb;

  .login-left {
    flex: 1;
    position: relative;

    .login-left-box {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 70%;
      height: 70%;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .login-right {
    padding: 20px;
    position: relative;
    width: 700px;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    display: flex;

    .login-right-logo {
      display: flex;
      align-items: center;
      margin-bottom: 30px;
      z-index: 1;

      img {
        width: 52px;
        height: 52px;
      }

      .login-right-logo-text {
        display: flex;
        flex-direction: row;

        &-span {
          margin-left: 10px;
          font-size: 28px;
          font-weight: bold;
          color: #fff;
        }
      }
    }

    .login-main {
      display: flex;
      flex-direction: column;
      border-radius: 3px;
      width: 500px;
      position: relative;
      overflow: hidden;
      padding: 20px 50px;
      background-color: #fff;

      .login-title {
        text-align: left;
        letter-spacing: 4px;

        span {
          font-size: 24px;
          font-weight: bold;
          color: var(--text-color-parmary);
        }

        p {
          padding-top: 10px;
          font-size: 14px;
          font-weight: 400;
          color: var(--color-danger-light-1);
        }
      }

      .login-form {
        margin: 10px 0;

        i {
          color: var(--dm-color-text-primary);
        }

        .el-form-item {
          margin-bottom: 20px !important;

          .login-code {
            display: flex;
            align-items: center;
            justify-content: space-around;
            margin: 0 0 0 10px;
            user-select: none;

            .login-code-img {
              margin-top: 2px;
              width: 100px;
              height: 38px;
              border: 1px solid var(--dm-border-color-base);
              color: var(--dm-color-text-primary);
              font-size: 14px;
              font-weight: 700;
              letter-spacing: 5px;
              line-height: 38px;
              text-indent: 5px;
              text-align: center;
              cursor: pointer;
              transition: all ease 0.2s;
              border-radius: 4px;

              &:hover {
                border-color: var(--dm-border-color-hover);
                transition: all ease 0.2s;
              }
            }
          }

          .login-submit {
            width: 100%;
            letter-spacing: 2px;
          }
        }
      }

      .login-right-warp-main-footer {
        padding: 0 50px 10px;
        text-align: center;
        color: var(--text-color-regular);
      }
    }
  }
}
</style>